//Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Collections.Generic;
using System.Text;

namespace Eas.WebCrawler.PlugIns.UrlProcessedHandlers {
    /// <summary>A delegate taking a <see cref="Eas.WebCrawler.Interfaces.UrlCompletedEventArgs"/> and returning a <see cref="Boolean"/>.</summary>
    /// <param name="argument">A <see cref="Eas.WebCrawler.Interfaces.UrlCompletedEventArgs"/>.</param>
    /// <returns><code>true</code> or <code>false</code>.</returns>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1711:IdentifiersShouldNotHaveIncorrectSuffix")]
    public delegate bool UrlSelectionDelegate(Eas.WebCrawler.Interfaces.UrlCompletedEventArgs argument);

    /// <summary>A processor that takes two nested processors and calls one or the other based on the result of calling into a passed-in delegate.</summary>
    public class ConditionalUrlProcessor : UrlProcessedHandler{

        /// <summary>Constructs a <see cref="ConditionalUrlProcessor"/> that two <paramref name="trueConditionHandler"/>s and a <see cref="UrlSelectionDelegate"/> that determines which of the two processors to call.</summary>
        /// <param name="trueConditionHandler">The handler to call if the <paramref name="conditionalDelegate"/> returns <code>true</code>.</param>
        /// <param name="falseConditionHandler">The handler to call if the <paramref name="conditionalDelegate"/> returns <code>false</code>.</param>
        /// <param name="conditionalDelegate">A <see cref="UrlSelectionDelegate"/> which determines which processor to call.</param>
        public ConditionalUrlProcessor(UrlProcessedHandler trueConditionHandler, UrlProcessedHandler falseConditionHandler, UrlSelectionDelegate conditionalDelegate) {
            _TrueConditionHandler = trueConditionHandler;
            _FalseConditionHandler = falseConditionHandler;
            _Delegate = conditionalDelegate;
        }

        /// <summary>A <see cref="UrlSelectionDelegate"/> which determines which processor to call when a Url is handled.</summary>
        private UrlSelectionDelegate _Delegate;

        /// <summary>The <see cref="UrlProcessedHandler"/> to use when the conditional delegate returns <code>true</code>.</summary>
        /// <remarks>Backing field for the TrueConditionCreator property.</remarks>
        /// <seealso cref="TrueConditionHandler"/>
        private UrlProcessedHandler _TrueConditionHandler;

        /// <summary>The <see cref="UrlProcessedHandler"/> to use when the conditional delegate returns <code>true</code>.</summary>
        public UrlProcessedHandler TrueConditionHandler {
            get { return _TrueConditionHandler; }
        }

        /// <summary>The <see cref="UrlProcessedHandler"/> to use when the conditional delegate returns <code>false</code>.</summary>
        /// <remarks>Backing field for the TrueConditionCreator property.</remarks>
        /// <seealso cref="FalseConditionHandler"/>
        private UrlProcessedHandler _FalseConditionHandler;

        /// <summary>The <see cref="UrlProcessedHandler"/> to use when the conditional delegate returns <code>false</code>.</summary>
        public UrlProcessedHandler FalseConditionHandler {
            get { return _FalseConditionHandler; }
        }

        /// <summary>Handles notification that aurl was processed.  It will delegate to an itnernal handler.</summary>
        /// <param name="sender">The sender of the notification.</param>
        /// <param name="argument">Details about the processed url.</param>
        public void UrlProcessed(object sender, Eas.WebCrawler.Interfaces.UrlCompletedEventArgs argument) {
            if (_Delegate(argument) ) {
                _TrueConditionHandler.UrlProcessed(sender, argument);
            }
            else {
                _FalseConditionHandler.UrlProcessed(sender, argument);
            }
        }

    }
}
